<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公司管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/position' }">公司岗位</el-breadcrumb-item>
      <el-breadcrumb-item>添加岗位</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 提示区 -->
      <el-alert title="添加岗位" type="info" center show-icon :closable="false"></el-alert>

      <el-form
        :model="addPositionForm"
        :rules="addPositionrules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item label="公司名称" prop="positionCompanyId">
          <el-select v-model="addPositionForm.positionCompanyId" placeholder="请选择公司">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="addPositionForm.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addPositionForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addPositionForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-row>
            <el-col v-for="item in addPositionForm.welfareList" :key="item" :span="24">
              <el-tag closable @close="handleClose(item)" type="primary">{{item}}</el-tag>
            </el-col>
            <el-col :span="12">
              <el-input
                class="input-new-tag"
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              ></el-input>
              <el-button v-else size="small" @click="showInput" type="primary" plain>+ 增加描述</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div align="center">
        <el-button class="btn" type="primary" @click="goAdd">添加岗位</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      companyList: [],
      addPositionForm: {
        welfareList: []
      },
      addPositionrules: {
        positionCompanyId: [{ required: true, message: '请选择公司', trigger: 'change' }],
        name: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入电话', trigger: 'blur' }]
      },
      inputVisible: false,
      inputValue: ''
    }
  },
  created() {
    this.getCompany()
  },
  methods: {
    handleClose(tag) {
      this.addPositionForm.welfareList.splice(this.addPositionForm.welfareList.indexOf(tag), 1)
      console.log(this.addPositionForm.welfareList)
    },
    async getCompany() {
      const { data: res } = await this.$http.get('/company', {
        params: {
          pageSize: -1,
          pageNum: -1
        }
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取公司列表失败！')
      }
      this.companyList = res.data.list
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      let inputValue = this.inputValue
      if (inputValue) {
        this.addPositionForm.welfareList.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    goAdd() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/position', this.addPositionForm)
        if (res.meta.status != 200) {
          return this.$message.error('新增岗位失败！')
        }
        this.$message.success('新增岗位成功！')
        this.$router.push('/position')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  margin: 30px 5px;
}
.btn {
  margin: 0 auto;
}
</style>